***********************************************
* Authors: Henrik Andersson & Sirus Dehdari
* This do-file generates all descriptive statistics; 
* all presented in the online appendix, section B    *
*************************

clear all
capture log close
set more off

log using "C:\Userdata\Shared\Logs\IntegrationSD\analysis", replace

use "E:\ProjData\IntegrationSD\use\main.dta"

///GENERATE SOME MORE VARIABLES

///THE POLITICAL MEASURE (PERCENT) ARE DIFFERENT IN 10 AND 14 - CHANGE
foreach y in Mproc Cproc FPproc KDproc Sproc Vproc MPproc SDproc FIproc PPproc {
replace `y' = `y'*100 if year==2014
}
*Dependent
gen dSD = d.SDproc

*Main independent
gen employment_share = employed_other/other_im
gen emp_proc_other = employment_share*100
gen unempl_other_share = unemployed_other/other_im
gen unem_proc_other = unempl_other_share*100
gen unem_proc_swed = (unemployed_swedish/swedish)*100

*Other variables
gen nordic_share = (nordic/pop)*100
gen swedish_share = (swedish/pop)*100
gen western_share = (western/pop)*100
gen europe_other_share = (europe_other/pop)*100
gen other_share = (other_im/pop)*100

gen low_educ_share = (low_educ/pop)*100
gen high_educ_share = (high_educ/pop)*100

gen pop2 = pop^2

gen ctz_other_share = (ctz_other/other_im)*100

*Workerdata
gen dother_high_educ = d.other_high_educ
gen other_high_educ_share = (other_high_educ/other_im)*100
gen other_low_educ_share = (other_low_educ/other_im)*100
gen dother_high_educ_share = d.other_high_educ_share

replace share_im_cworker_swe = share_im_cworker_swe*100

replace share_im_cworker_swe_stayed = share_im_cworker_swe_stayed*100
replace sh_im_cw_noi_swe = sh_im_cw_noi_swe*100
replace share_male_cw_wp = share_male_cw_wp*100
replace share_young_cw_wp = share_young_cw_wp*100

///wage
gen logwage = ln(LoneInk)
///regions
foreach x in latin mena asia africa rest nordic western {
replace share_`x'_cworker_swe = share_`x'_cworker_swe*100
}

///TYPE YEAR
foreach x in newim mediumnewim notnewim oldim highskother lowskother {
replace share_`x'_cworker_swe = share_`x'_cworker_swe*100
}

*Municipality
gen str8 str_dstr = string(precinct2014,"%08.0f")
gen kom_str = substr(str_dstr, 1,4)
destring kom_str, replace

*Region 
gen lan_str = substr(str_dstr, 1,2)
destring lan_str, replace

*LABELS

label variable SDproc  "$\%$ votes for the SD"
label variable dSD  "Votes for the SD, $\Delta$ $\%$-units"
label variable share_im_cworker_swe  "$\%$ non-European coworkers \\among native precinct residents"
label variable im_coworker_swe  "$\#$ imm-coworkers among Swedes"
label variable pop "Population"
label variable low_educ_share "$\%$ low educated"
label variable ALosDag  "$\#$ of unemployment benefit days"
label variable ctz_other_share  "$\%$ citizens of non-Europeans"
label variable unem_proc_swed  "$\%$ non-working swedes"
label variable other_share  "$\%$ non-Europeans in precinct"
label variable other_low_educ_share  "$\%$ low educ. non-Europeans in precinct"

///worker data controls
label variable wage_cw_wp "Wage of coworkers \\among precinct residents"
label variable share_male_cw_wp "$\%$ males of coworkers \\among precinct residents"
label variable share_young_cw_wp "$\%$ young of coworkers \\among precinct residents"
*regions
label variable share_latin_cworker_swe  "$\%$ Latin American"
label variable latin_coworker_swe  "# Latin American"
label variable share_mena_cworker_swe  "$\%$ MENA"
label variable mena_coworker_swe  "# MENA"
label variable share_asia_cworker_swe  "$\%$ Other Asia"
label variable asia_coworker_swe  "# Other Asia"
label variable share_africa_cworker_swe  "$\%$ Other Africa"
label variable africa_coworker_swe  "# Other Africa"
label variable share_rest_cworker_swe  "$\%$ Oceania and Stateless"
label variable rest_coworker_swe  "# Oceania and Stateless"
  
label variable share_nordic_cworker_swe  "$\%$ Nordic"
label variable nordic_coworker_swe  "# Nordic"
label variable share_western_cworker_swe  "$\%$ Western"
label variable western_coworker_swe  "# Western"

*other worker characteristics
label variable share_newim_cworker_swe  "$\leq$5 years in country"
label variable share_mediumnewim_cworker_swe "$\leq$10 years, $\geq$5 years in country"
label variable share_notnewim_cworker_swe "$\leq$15 years, $\geq$10 years in country"
label variable share_oldim_cworker_swe  "$\geq$15 years in country"                

label variable share_highskother_cworker_swe "$\%$ high skilled"
label variable share_lowskother_cworker_swe "$\%$ low skilled"


///SOME DESCRIPTIVES 

 
///SD VOTES, SPREAD PER PRECINCT (Figure B1)
bysort year: sum SDproc
bysort year: sum SDproc, d
hist SDproc, by(year) 
twoway (hist SDproc if year==2006, color(black) recast(line) lp(shortdash_dot)) /// 
(hist SDproc if year==2010, color(teal) recast(line) lp(solid)) ///
(hist SDproc if year==2014, color(navy) recast(line) lp(dash)), ///
legend(ring(0) position(3) order(1 "2006" 2 "2010" 3 "2014")) graphregion(color(white)) xtitle("Percentage of votes for SD") ///
ytitle(Density)

graph export "C:\Userdata\Shared\Output\IntegrationSD\SDincrease.pdf", replace
graph export "C:\Users\henrik\Dropbox\IntregrationSD\Output\Figures\SDincrease.pdf", replace


///Change in share of non-Europeans (Figure B2)
sort precinct2014 t
gen dtreat= d.share_im_cworker_swe
hist dtreat, color(navy) graphregion(color(white)) xtitle("Percentage point change non-European co-workers")
graph export "C:\Userdata\Shared\Output\IntegrationSD\increaseimmcoworkers.pdf", replace
graph export "C:\Users\henrik\Dropbox\IntregrationSD\Output\Figures\increaseimmcoworkers.pdf", replace


*hit the four percent break
*bysort year: count if SDproc>4
*count if year==2006
*count if year==2010
*count if year==2014


///An overall summary stats table (Table B1)
estpost tabstat SDproc dSD sh_im_cw_noi_swe ///
pop low_educ_share unem_proc_swed logwage  ALosDag  /// 
 other_share ctz_other_share other_low_educ_share ///
 wage_cw_wp share_male_cw_wp share_young_cw_wp , ///
statistics(mean sd min max n) columns(statistics) 

esttab using "C:\Userdata\Shared\Output\IntegrationSD\sumstat.tex", /// 
nonumbers float noobs label cells("mean(fmt(2)) sd min max count(fmt(0))") ///
refcat(SDproc "\textbf{Outcome}" share_im_cworker_swe " &  &  & &  & \\ \textbf{Treatment}" pop " &  &  & &  & \\ \textbf{Controls}", nolabel) replace 
esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\sumstat.tex", /// 
nonumbers float noobs label cells("mean(fmt(2)) sd min max count(fmt(0))") ///
refcat(SDproc "\textbf{Outcome}" share_im_cworker_swe " &  &  & &  & \\ \textbf{Treatment}" pop " &  &  & &  & \\ \textbf{Controls}", nolabel) replace 

///NO FLOAT
esttab using "C:\Userdata\Shared\Output\IntegrationSD\sumstatnofloat.tex", /// 
nonumbers fragment noobs label cells("mean(fmt(2)) sd min max count(fmt(0))") ///
refcat(SDproc " &  &  & &  & \\ \textbf{Outcome}" share_im_cworker_swe " &  &  & &  & \\ \textbf{Treatment}" pop " &  &  & &  & \\ \textbf{Controls}", nolabel) replace 
esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\sumstatnofloat.tex", /// 
nonumbers fragment noobs label cells("mean(fmt(2)) sd min max count(fmt(0))") ///
refcat(SDproc " &  &  & &  & \\ \textbf{Outcome}" share_im_cworker_swe " &  &  & &  & \\ \textbf{Treatment}" pop " &  &  & &  & \\ \textbf{Controls}", nolabel) replace 





///Some more detailed about the workplace (Table B4)
estpost tabstat share_im_cworker_swe share_newim_cworker_swe ///
share_mediumnewim_cworker_swe share_notnewim_cworker_swe ///
share_oldim_cworker_swe share_highskother_cworker_swe share_lowskother_cworker_swe ///
share_latin_cworker_swe share_mena_cworker_swe /// 
share_asia_cworker_swe share_africa_cworker_swe share_rest_cworker_swe ///
, statistics(mean sd min max n) columns(statistics) 

esttab using "C:\Userdata\Shared\Output\IntegrationSD\regions_coworkers.tex", /// 
nonumbers noobs float label cells("mean(fmt(2)) sd min max count(fmt(0))") /// 
title("Immigrant coworkers and their characteristics") ///
refcat(share_newim_cworker_swe " &  &  & &  & \\ \textbf{Separated by year in country}" ///
share_highskother_cworker_swe " &  &  & &  & \\ \textbf{Separated by skill level}" ///
share_latin_cworker_swe " &  &  & &  & \\ \textbf{Separated by origin}", nolabel) replace 

esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\regions_coworkers.tex", /// 
nonumbers noobs float label cells("mean(fmt(2)) sd min max count(fmt(0))") ///
title("Immigrant coworkers and their characteristics") ///
refcat(share_newim_cworker_swe " &  &  & &  & \\ \textbf{Separated by year in country}" ///
share_highskother_cworker_swe " &  &  & &  & \\ \textbf{Separated by skill level}" ///
share_latin_cworker_swe " &  &  & &  & \\ \textbf{Separated by origin}", nolabel) replace  


///NO FLOAT
esttab using "C:\Userdata\Shared\Output\IntegrationSD\regions_coworkersnofloat.tex", /// 
nonumbers fragment noobs label cells("mean(fmt(2)) sd min max count(fmt(0))")  ///
refcat(share_newim_cworker_swe " &  &  & &  & \\ \textbf{Separated by year in country}" ///
share_highskother_cworker_swe " &  &  & &  & \\ \textbf{Separated by skill level}" ///
share_latin_cworker_swe " &  &  & &  & \\ \textbf{Separated by origin}", nolabel) replace  
esttab using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\regions_coworkersnofloat.tex", /// 
nonumbers fragment noobs label cells("mean(fmt(2)) sd min max count(fmt(0))") ///
refcat(share_newim_cworker_swe " &  &  & &  & \\ \textbf{Separated by year in country}" ///
share_highskother_cworker_swe " &  &  & &  & \\ \textbf{Separated by skill level}" ///
share_latin_cworker_swe " &  &  & &  & \\ \textbf{Separated by origin}", nolabel) replace  




///Correlation matrix fixed effects and controls (Table B3)

eststo: xtreg SDproc, fe vce(cluster precinct2014)
predict fe, u


estpost corr fe pop pop2 logwage low_educ_share ALosDag ctz_other_share unem_proc_swed other_share other_low_educ_share, matrix
eststo correlation
esttab correlation using "C:\Users\henrik\Dropbox\IntregrationSD\Output\Tables\correlation_mat.tex", unstack nostar compress b(2) label noobs replace




///OTHER STATS NOT IN THE PAPER:
 ///how big are the districts?
 ///fairly compressed, 10p-90p - 825-1600 people
 sum pop, d
 
 


